{% extends "base.html" %} {% block content %} {% load custom_filter %}

<div class="row">
    <div class="col-md-12">
        <!-- Horizontal Form -->
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title"></h3>
                <small>最后更新时间[{{ main_config.config_id }}]：{{ main_config.update_time | timestamp_to_date }}</small>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form class="form-horizontal" id="form_main_config">
                <div class="box-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label">进程数量</label>
                        <div class="col-sm-9">
                            <div id="div_worker_processes">
                                <input type="text" class="form-control" name="worker_processes" id="input_worker_processes" placeholder="用于处理请求的进程数量，建议参照 CPU 核心数量（1-99）" value="{{ main_config.worker_processes }}" onblur="check_form(this)" data-inputmask="'mask':'[9]{1,2}'" data-mask>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" onchange="check_auto_process()" name="auto_worker_processes" id="check_worker_processes"> 自动检测（CPU 核心数量）
                                </label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">最大连接数</label>
                        <div class="col-sm-9">
                            <div id="div_worker_connections">
                                <input type="text" class="form-control" name="worker_connections" id="input_worker_connections" placeholder="系统支持的最大并发连接数（1-99999）" value="{{ main_config.worker_connections }}" onblur="check_form(this)" data-inputmask="'mask':'[9]{1,5}'" data-mask>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">超时时间</label>
                        <div class="col-sm-9">
                            <div class="input-group">
                                <div id="div_keepalive_timeout">
                                    <input type="text" class="form-control" name="keepalive_timeout" id="input_keepalive_timeout" placeholder="请求超时时间（1-999）" value="{{ main_config.keepalive_timeout }}" onblur="check_form(this)" data-inputmask="'mask':'[9]{1,3}'" data-mask>
                                </div>
                                <div class="input-group-addon">
                                    <span><b>秒</b></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">请求正文大小</label>
                        <div class="col-sm-9">
                            <div class="input-group">
                                <div id="div_client_max_body_size">
                                    <input type="text" class="form-control" name="client_max_body_size" id="input_client_max_body_size" placeholder="请求正文大小限制（1-999）" value="{{ main_config.client_max_body_size }}" onblur="check_form(this)" data-inputmask="'mask':'[9]{1,3}'" data-mask>
                                </div>
                                <div class="input-group-addon">
                                    <span><b>MB</b></span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">访问日志</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="access_log" id="input_access_log" placeholder="全局请求日志路径" value="{{ main_config.access_log }}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label">错误日志</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" name="error_log" id="input_error_log" placeholder="全局错误日志路径" value="{{ main_config.error_log }}">
                        </div>
                    </div>

                </div>
                <!-- /.box-body -->
                <div class="box-footer">
                    <!-- <button type="submit" class="btn btn-default">Cancel</button> -->
                    <button type="button" class="btn btn-info pull-right" onclick="save_config()">保存</button>
                </div>
                <!-- /.box-footer -->
            </form>
        </div>
        <!-- /.box -->
    </div>
</div>


<script>
    //init page
    $('#menu_main_config').addClass('active')
    $('#page_header').text('全局配置')
    $('#page_header_descript').text('用于管理 Nginx 全局配置')
    $('#page_nav').text('服务管理')
    $('#page_name').text('全局配置')

    //input filter
    $("[data-mask]").inputmask();


    function check_auto_process() {
        if ($('#check_worker_processes')[0].checked == true) {
            $('#input_worker_processes').val('')
            $('#input_worker_processes').attr('disabled', 'disabled')
            $('#div_worker_processes').removeClass('has-error')
            $('#div_worker_processes').addClass('has-success')
        } else {
            $('#input_worker_processes').removeAttr('disabled')
            $('#div_worker_processes').removeClass('has-success')
            $('#div_worker_processes').addClass('has-error')
        }
    }

    if ($('#input_worker_processes').val() == "0"){
        $('#check_worker_processes').prop('checked','checked')
        $('#input_worker_processes').val('')
        $('#input_worker_processes').attr('disabled', 'disabled')
    }

    function check_form(_input) {
        var _input_div = $(_input.closest("div"))
        _input_div.removeClass('has-error')
        _input_div.removeClass('has-success')
        if ($(_input).val() == "") {
            _input_div.addClass('has-error')
        } else {
            _input_div.addClass('has-success')
        }
    }

    function save_config() {
        if (confirm("确认要保存这样的配置？")) {
            jQuery.ajax({
                type: 'post',
                url: '/main/save/',
                data: JSON.stringify($('#form_main_config').serializeObject()),
                dataType: 'json',
                success: function(p) {
                    if (p.flag == "Success") {
                        alert("保存成功!")
                        top.location = '/main/'
                    } else if (p.flag == "Error" && p.context == "ArgsError") {
                        alert('保存失败！配置项填写错误！')
                    } else if (p.flag == "Error" && p.context == "AuthFailed") {
                        alert('认证失败！请重新登录！')
                        top.location = '/login/'
                    } else {
                        alert('保存错误！其他错误：' + p.context)
                    }
                },
                error: function(e) {
                    alert('请求失败!')
                }
            })
        }
    }
</script>
{% endblock %}
